package cn.iocoder.yudao.module.cmt.controller.admin.base;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cmt.controller.admin.base.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.base.BaseMerchantStaffDO;
import cn.iocoder.yudao.module.cmt.service.base.BaseMerchantStaffService;

@Tag(name = "管理后台 - 商户员工")
@RestController
@RequestMapping("/cmt/base-merchant-staff")
@Validated
public class BaseMerchantStaffController {

    @Resource
    private BaseMerchantStaffService merchantStaffService;

    @PostMapping("/create")
    @Operation(summary = "创建商户员工")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-staff:create')")
    public CommonResult<Long> createMerchantStaff(@Valid @RequestBody BaseMerchantStaffSaveReqVO createReqVO) {
        return success(merchantStaffService.createMerchantStaff(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户员工")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-staff:update')")
    public CommonResult<Boolean> updateMerchantStaff(@Valid @RequestBody BaseMerchantStaffSaveReqVO updateReqVO) {
        merchantStaffService.updateMerchantStaff(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商户员工")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-staff:delete')")
    public CommonResult<Boolean> deleteMerchantStaff(@RequestParam("id") Long id) {
        merchantStaffService.deleteMerchantStaff(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除商户员工")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-staff:delete')")
    public CommonResult<Boolean> deleteMerchantStaffList(@RequestParam("ids") List<Long> ids) {
        merchantStaffService.deleteMerchantStaffListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商户员工")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-staff:query')")
    public CommonResult<BaseMerchantStaffRespVO> getMerchantStaff(@RequestParam("id") Long id) {
        BaseMerchantStaffDO merchantStaff = merchantStaffService.getMerchantStaff(id);
        return success(BeanUtils.toBean(merchantStaff, BaseMerchantStaffRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商户员工分页")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-staff:query')")
    public CommonResult<PageResult<BaseMerchantStaffRespVO>> getMerchantStaffPage(@Valid BaseMerchantStaffPageReqVO pageReqVO) {
        PageResult<BaseMerchantStaffDO> pageResult = merchantStaffService.getMerchantStaffPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseMerchantStaffRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商户员工 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:base-merchant-staff:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMerchantStaffExcel(@Valid BaseMerchantStaffPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseMerchantStaffDO> list = merchantStaffService.getMerchantStaffPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商户员工.xls", "数据", BaseMerchantStaffRespVO.class,
                BeanUtils.toBean(list, BaseMerchantStaffRespVO.class));
    }

}
